package com.community.zhihuishequ.view.home;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.amulyakhare.textdrawable.TextDrawable;
import com.blankj.utilcode.util.SizeUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.community.zhihuishequ.R;
import com.community.zhihuishequ.utils.ChartUtils;
import com.community.zhihuishequ.utils.ViewUtil;
import com.community.zhihuishequ.view.base.MultiItem;
import com.community.zhihuishequ.view.car.CarManagerListActivity;
import com.community.zhihuishequ.view.collect.CollectManagerActivity;
import com.community.zhihuishequ.view.defense.DefenseListActivity;
import com.community.zhihuishequ.view.monitor.MonitorActivity;
import com.community.zhihuishequ.view.personnel.PersonnelListActivity;
import com.community.zhihuishequ.view.record.RecordListActivity;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.utils.Utils;
import com.github.mikephil.charting.utils.ViewPortHandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 首页内容
 */
public class SubSystemFragment extends Fragment {

    private static final int TYPE_HEADER = 1;
    private static final int TYPE_ITEM = 2;
    private static final int TYPE_SPACE = 3;

    @BindView(R.id.txvTitle)
    TextView txvTitle;
    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.rcvContent)
    RecyclerView rcvContent;
    Unbinder unbinder;
    @BindView(R.id.line_chart_visitor)
    LineChart lineChartVisitor;
    @BindView(R.id.tv_today_visitor)
    TextView tvTodayVisitor;
    @BindView(R.id.tv_yesterday_visitor)
    TextView tvYesterdayVisitor;
    @BindView(R.id.tv_week_visitor)
    TextView tvWeekVisitor;


    private ItemAdapter itemAdapter;
    private int fontSize;
    private int drawableSize;
    private int itemSize;
    private TextDrawable.IShapeBuilder draeableBuilder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_home_subsystem, container, false);
        unbinder = ButterKnife.bind(this, v);
        init();
        initLineChart();
        return v;
    }

    private void init() {
        txvTitle.setText("来访人数");
        rcvContent.setLayoutManager(new GridLayoutManager(getContext(), 3));
        itemAdapter = new ItemAdapter(getItems());
        rcvContent.setAdapter(itemAdapter);
        fontSize = SizeUtils.sp2px(24);
        drawableSize = (int) Math.sqrt(fontSize * fontSize * 2f);
        draeableBuilder = TextDrawable.builder()
                .beginConfig()
                .bold()
                .fontSize(fontSize)
                .textColor(Color.WHITE)
                .width(drawableSize)
                .height(drawableSize)
                .endConfig();
        itemSize = getResources().getDisplayMetrics().widthPixels / 3;
    }

    private void initLineChart() {
        ChartUtils.initChart(lineChartVisitor);
        ChartUtils.notifyDataSetChanged(lineChartVisitor, getData(), ChartUtils.weekValue);
    }

    private List<Entry> getData() {
        List<Entry> values = new ArrayList<>();
        values.add(new Entry(0, 10));
        values.add(new Entry(1, 14));
        values.add(new Entry(2, 12));
        values.add(new Entry(3, 16));
        values.add(new Entry(4, 17));
        values.add(new Entry(5, 11));
        values.add(new Entry(6, 13));
        return values;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private List<MultiItem> getItems() {
        List<MultiItem> datas = new ArrayList<>();
        datas.add(new MultiItem(TYPE_HEADER, "社区安防"));
        datas.add(new MultiItem(TYPE_ITEM, "视频监控", R.drawable.ic_monitor));
        datas.add(new MultiItem(TYPE_ITEM, "周界防范", R.drawable.ic_defense));
        datas.add(new MultiItem(TYPE_SPACE, ""));
        datas.add(new MultiItem(TYPE_HEADER, "车辆管理"));
        datas.add(new MultiItem(TYPE_ITEM, "车辆进出", R.drawable.ic_car));
        datas.add(new MultiItem(TYPE_SPACE, ""));
        datas.add(new MultiItem(TYPE_SPACE, ""));
        datas.add(new MultiItem(TYPE_HEADER, "人员管理"));
        datas.add(new MultiItem(TYPE_ITEM, "人员进出", R.drawable.ic_personnel));
        datas.add(new MultiItem(TYPE_ITEM, "信息采集", R.drawable.ic_collect));
        datas.add(new MultiItem(TYPE_ITEM, "社区档案", R.drawable.ic_record));
        return datas;
    }

    private class ItemAdapter extends BaseMultiItemQuickAdapter<MultiItem, BaseViewHolder> {

        private int count = 0;

        /**
         * Same as QuickAdapter#QuickAdapter(Context,int) but with
         * some initialization data.
         *
         * @param data A new list is created out of this one to avoid mutable list
         */
        public ItemAdapter(List<MultiItem> data) {
            super(data);
            addItemType(TYPE_HEADER, R.layout.listitem_subsystem_header);
            addItemType(TYPE_ITEM, R.layout.listitem_subsystem_item);
            addItemType(TYPE_SPACE, R.layout.listitem_subsystem_space);
            setSpanSizeLookup(new SpanSizeLookup() {
                @Override
                public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
                    switch (getDefItemViewType(position)) {
                        case TYPE_HEADER:
                            return 3;
                        default:
                            return 1;
                    }
                }
            });
        }

        @Override
        protected void convert(BaseViewHolder helper, final MultiItem item) {
            if (item.getItemType() == TYPE_HEADER) {
                helper.setText(R.id.txvTitle, (String) item.getData());
                count = 0;
            } else if (item.getItemType() == TYPE_ITEM) {
                count = count + 1;
                if (count == 3) {
                    helper.setVisible(R.id.line, false);
                } else {
                    count = 0;
                }
                RecyclerView.LayoutParams p = (RecyclerView.LayoutParams) helper.itemView.getLayoutParams();
                p.height = itemSize;
                helper.itemView.setLayoutParams(p);
                String title = item.getData();
                int resId = item.getData2();
                TextView v = helper.getView(R.id.txvTitle);
                v.setText(title);
                ViewUtil.setTextDrawable(v, 0, resId, 0, 0);
                helper.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        typeItemClickListener(item);
                    }
                });
            }
        }
    }

    private void typeItemClickListener(MultiItem i) {
        if (i == null) {
            return;
        }
        String data = i.getData();
        if (data == null) {
            return;
        }
        if (data.equals("视频监控")) {
            startActivity(new Intent(getActivity(), MonitorActivity.class));
        } else if (data.equals("周界防范")) {
            DefenseListActivity.startActivity(getActivity());
        } else if (data.equals("车辆进出")) {
            startActivity(new Intent(getActivity(), CarManagerListActivity.class));
        } else if (data.equals("人员进出")) {
            startActivity(new Intent(getActivity(), PersonnelListActivity.class));
        } else if (data.equals("信息采集")) {
            startActivity(new Intent(getActivity(), CollectManagerActivity.class));
        } else if (data.equals("社区档案")) {
            startActivity(new Intent(getActivity(), RecordListActivity.class));
        }
    }
}
